const Router = require("@koa/router");
const Server = require("../utils/httpServer");
const resultObj = require("../utils/resultCreator");
const newsRouter = new Router({
  prefix: "/news",
});
newsRouter.get('/:type',async function (ctx) {
  const type = parseInt(ctx.params.type)
  let path
  // 0 热点新闻 1 社会新闻 2 娱乐新闻 3体育新闻 4美文 散文 5科技 6 财经 7 时尚
  switch (type) {
    case 0:
      path =
        '/list/?tag=news_hot&ac=wap&count=20&format=json_raw&as=A1A59982B911729&cp=5929E12752796E1&min_behot_time=0'
      break
    case 1:
      path =
        '/list/?tag=news_society&ac=wap&count=20&format=json_raw&as=A195B9F229018CD&cp=592991783C9D8E1&min_behot_time=0'
      break
    case 2:
      path =
        '/list/?tag=news_entertainment&ac=wap&count=20&format=json_raw&as=A1C51992996195E&cp=5929D119B58EFE1&min_behot_time=0'
      break
    case 3:
      path =
        '/list/?tag=news_sports&ac=wap&count=20&format=json_raw&as=A1054902B911A1E&cp=592991AA81AEAE1&min_behot_time=0'
      break
    case 4:
      path =
        '/list/?tag=news_essay&ac=wap&count=20&format=json_raw&as=A195495279C19DE&cp=5929C1F91DFEEE1&min_behot_time=0'
      break
    case 5:
      path =
        '/list/?tag=news_tech&ac=wap&count=20&format=json_raw&as=A1854972BABC6FF&cp=592A9CC64FCFAE1&max_behot_time=0'
      break
    case 6:
      path =
        '/list/?tag=news_finance&ac=wap&count=20&format=json_raw&as=A145E9025A6C78B&cp=592ACC87687B1E1&max_behot_time=0'
      break

    case 7:
      path =
        '/list/?tag=news_fashion&ac=wap&count=20&format=json_raw&as=A1353902AA9C7F9&cp=592ADCD7CF89AE1&max_behot_time=0'
      break
    default:
      path =
        '/list/?tag=news_hot&ac=wap&count=20&format=json_raw&as=A1A59982B911729&cp=5929E12752796E1&min_behot_time=0'
  }

  const host = 'm.toutiao.com'
  // false:http请求  true:https请求
  const headers = {
    Cookie: 'tt_webid=6819320381281076749;domian=m.toutiao.com'
  }
  try {
    const body = await Server.httpGet(host, {}, path, true, headers);
    ctx.body = resultObj(200, "查询成功", JSON.parse(body).data);
  } catch (error) {
    console.log(error);
    ctx.body = resultObj(404, "网络错误", error);
  }
})

newsRouter.get("/detail/:newsId", async function (ctx) {
  const itemId = ctx.params.newsId || "6424603234748334594";
  const host = "m.toutiao.com";
  const path = `/i${itemId}/info/`;
  // false:http请求  true:https请求
  console.log(path);
  try {
    const body = await Server.httpGet(host, {}, path, true);
    ctx.body = resultObj(200, "查询成功", JSON.parse(body).data);
  } catch (error) {
    console.log(error);
    ctx.body = resultObj(404, "网络错误", error);
  }
});
newsRouter.get("/:type", function (req, res) {
  const type = parseInt(req.query.type);
  let path;
  // 0 热点新闻 1 社会新闻 2 娱乐新闻 3体育新闻 4美文 散文 5科技 6 财经 7 时尚
  switch (type) {
    case 0:
      path =
        "/list/?tag=news_hot&ac=wap&count=20&format=json_raw&as=A1A59982B911729&cp=5929E12752796E1&min_behot_time=0";
      break;
    case 1:
      path =
        "/list/?tag=news_society&ac=wap&count=20&format=json_raw&as=A195B9F229018CD&cp=592991783C9D8E1&min_behot_time=0";
      break;
    case 2:
      path =
        "/list/?tag=news_entertainment&ac=wap&count=20&format=json_raw&as=A1C51992996195E&cp=5929D119B58EFE1&min_behot_time=0";
      break;
    case 3:
      path =
        "/list/?tag=news_sports&ac=wap&count=20&format=json_raw&as=A1054902B911A1E&cp=592991AA81AEAE1&min_behot_time=0";
      break;
    case 4:
      path =
        "/list/?tag=news_essay&ac=wap&count=20&format=json_raw&as=A195495279C19DE&cp=5929C1F91DFEEE1&min_behot_time=0";
      break;
    case 5:
      path =
        "/list/?tag=news_tech&ac=wap&count=20&format=json_raw&as=A1854972BABC6FF&cp=592A9CC64FCFAE1&max_behot_time=0";
      break;
    case 6:
      path =
        "/list/?tag=news_finance&ac=wap&count=20&format=json_raw&as=A145E9025A6C78B&cp=592ACC87687B1E1&max_behot_time=0";
      break;

    case 7:
      path =
        "/list/?tag=news_fashion&ac=wap&count=20&format=json_raw&as=A1353902AA9C7F9&cp=592ADCD7CF89AE1&max_behot_time=0";
      break;
    default:
      path =
        "/list/?tag=news_hot&ac=wap&count=20&format=json_raw&as=A1A59982B911729&cp=5929E12752796E1&min_behot_time=0";
  }

  const host = "m.toutiao.com";
  // false:http请求  true:https请求
  console.log("m.toutiao.com" + path);
  const headers = {
    Cookie: "tt_webid=6819320381281076749;domian=m.toutiao.com",
  };
  Server.httpGet(host, {}, path, true, headers)
    .then(function (body) {
      res.send({
        code: 200,
        data: JSON.parse(body).data,
        msg: "",
      });
    })
    .catch(function (err) {
      res.send({
        code: 404,
        msg: "网络好像有点问题",
      });
      console.log(err);
    });
});
newsRouter.get("/:type/:page", function (req, res) {
  let id;
  const type = parseInt(req.params.type);
  const page = parseInt(req.params.page);
  // 0搞笑视频  1美女视频  2体育视频  3 新闻现场 4涨姿势  5猎奇  6 黑科技 默认搞笑视频
  switch (type) {
    case 0:
      id = "VAP4BFE3U";
      break;
    case 1:
      id = "VAP4BG6DL";
      break;
    case 2:
      id = "VBF8F2E94";
      break;
    case 3:
      id = "VAV3H6JSN";
      break;
    case 4:
      id = "VBF8F3SGL";
      break;
    case 5:
      id = "VBF8ET3S2";
      break;
    case 6:
      id = "VBF8F2PKF";
      break;
    default:
      id = "VAP4BFE3U";
  }

  const host = "c.m.163.com";
  const path = `/nc/video/list/${id}/y/${page}-20.html`;
  // false:http请求  true:https请求
  console.log(path);
  Server.httpGet(host, {}, path, true)
    .then(function (body) {
      const arr = JSON.parse(body);
      res.send({
        code: 200,
        data: arr[id],
        msg: "",
      });
    })
    .catch(function (err) {
      res.send({
        code: 404,
        msg: "网络好像有点问题",
      });
      console.log(err);
    });
});
module.exports = newsRouter;
